package com.example.gamestore.control;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.gamestore.Bean.Address;
import com.example.gamestore.Bean.Game;
import com.example.gamestore.Bean.OrderItem;
import com.example.gamestore.Bean.Orders;
import com.example.gamestore.aop.AdminLogAnnotation;
import com.example.gamestore.mapper.AddressMapper;
import com.example.gamestore.mapper.OrderItemMapper;
import com.example.gamestore.mapper.OrderMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class AdminOrderControl {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Autowired
    private AddressMapper addressMapper;

    @GetMapping("/admin/order")
    public String order(Model model, @RequestParam(name = "pn", defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 5);
        List<Orders> ordersList = orderMapper.selectList(null);
        PageInfo<Orders> pageInfo = new PageInfo<>(ordersList, 5);
        model.addAttribute("pages", pageInfo);
        return "Admin/order";
    }

    @PostMapping("/admin/fahuo")
    @AdminLogAnnotation(module = "订单模块",operater = "改变订单状态")
    @ResponseBody
    public String xiajia(@RequestParam(name = "orderid") String orderid) {
        LambdaUpdateWrapper<Orders> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Orders::getOrderId, orderid);
        Orders orders = orderMapper.selectById(orderid);
        Orders updatestatus = updatestatus(orders);
        orderMapper.update(updatestatus,wrapper);
        return "修改成功";
    }
    @GetMapping("/admin/orderdetail")
    public String detail(Model model,String OrderId){
        Orders orders = orderMapper.selectById(OrderId);
        LambdaQueryWrapper<OrderItem> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(OrderItem::getOrderId,OrderId);
        List<OrderItem> orderItems = orderItemMapper.selectList(wrapper);
        Address address = addressMapper.selectById(OrderId);
        model.addAttribute("order",orders).addAttribute("orderItems",orderItems)
                .addAttribute("address",address);
        return "Admin/Orderdetail";
    }

    /**
     * 修改订单状态
     *
     * @param orders
     * @return
     */
    public Orders updatestatus(Orders orders) {
        Integer status = orders.getStatus();
        if (status == 0) {
            orders.setStatus(1);
        }
        if (status == 1) {
            orders.setStatus(2);
        }
        return orders;
    }

}
